<template>
    <Return />
    <div class="container">
        <el-alert title="评论失败" type="error" show-icon :closable="false" v-show="controlEle.error" class="error" />
        <el-alert title="评论成功" type="success" show-icon :closable="false" v-show="controlEle.success" class="error" />
        <div class="title">
            <p>实践日志</p>
            <p>提交时间: {{ /..........[\s]/.exec(curData.commitDate)[0] }}</p>
        </div>
        <div  ref="text" id="text" class="content demo-1">
            
        </div>
        <p class="subTitle">附件:</p>
        <div class="fileBox demo-1">
            <li v-for="(item, index) in curData.files">
                <img :src="fileImg" alt="" @click="uploadFile(item.fileId, item.fileName)">
                <div class="fileName">
                    {{ item.fileName }}
                </div>
            </li>
        </div>
        <div class="but">
            <button @click="uploadAllFile">全部下载</button>
        </div>
        <p class="subTitle">评价:</p>
        <div class="commend">
            <textarea name="" placeholder="" ref="comment" cols="30" rows="10">{{ curData.teacherComment }}</textarea>
        </div>
        <div class="but" style="margin-top: 1.4%;">
            <button style="height: 37px;" @click="updateComment">提交</button>
        </div>
    </div>
</template>
<script setup>
import Return from '../../../components/Return.vue'
import { createElementBlock, getCurrentInstance, onMounted, reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getUserData } from '../../../utils';
import fileImg from '../../../assets/images/teacher/public/file.svg'
import { Flag } from '@element-plus/icons-vue';

const http = getCurrentInstance()?.appContext.config.globalProperties.$http
const route = useRoute()
const router = useRouter()

// 获得用户信息
// 获得用户信息
let userData = {
    Token: '',
    avatar: '',
    id: '',
    role: ''
}
if (getUserData() != undefined) {
    userData = getUserData();
}

//当前页面信息
let curData = ref()

//得到对应relationId
let lid = route.params.lid;

//得到学生页面信息
fetch(http + '/tec/guide/student/log/' + lid, {
    method: 'get',
    headers: {
        "Token": userData.Token,
        'Content-Type': 'application/json; charset=utf-8'
    },
    responseType: 'json',
}).then((response) => {
    return response.json()
}).then((data) => {
    data = data.data
    curData.value = data
    console.log(curData.value);
}).catch((err) => {
    console.log(err);
})

const text = ref()
onMounted(()=>{
    text.value.innerHTML = curData.value.text
    console.log(text);
})

// 下载文件
const uploadFile = function (fid, fName) {
    fetch(http + '/user/download/' + fid, {
        method: 'get',
        headers: {
            "Token": userData.Token,
            'Content-Type': 'application/json; charset=utf-8'
        },
        responseType: 'blob',
    }).then((response) => {
        return response.blob()
    }).then((data) => {
        console.log(data);
        let a = document.createElement('a') 
        a.href = window.URL.createObjectURL(data)
        a.setAttribute("download", fName)
        let body = document.querySelector('body')
        body.appendChild(a)
        a.click()
        body.removeChild(a)
        window.URL.revokeObjectURL(data)
    }).catch((err) => {
        console.log(err);
    })
}

// 下载所有文件
const uploadAllFile = function(){
    console.log(lid)
    fetch(http + '/tec/guide/student/log/'+lid+'/file', {
        method: 'get',
        headers: {
            "Token": userData.Token,
            'Content-Type': 'application/json; charset=utf-8'
        },
        responseType: 'blob',
    }).then((response) => {
        return response.blob()
    }).then((data) => {
        console.log(data);
        let a = document.createElement('a') 
        a.href = window.URL.createObjectURL(data)
        a.setAttribute("download", '实践日志附件.zip')
        let body = document.querySelector('body')
        body.appendChild(a)
        a.click()
        body.removeChild(a)
        window.URL.revokeObjectURL(data)
    }).catch((err) => {
        console.log(err);
    })
}

//提醒评论成功与否
const  controlEle = reactive({
    error: false,
    success: false
})

// 显示提醒
const showHint = function(type){
    if(type == 'err'){
        controlEle.error = true
        setTimeout(()=>{
            controlEle.error = false
        }, 5000)
    }else{
        controlEle.success = true
        setTimeout(()=>{
            controlEle.success = false
        }, 5000)
    }
}

// 更新评论
const comment = ref()
const updateComment = function () {
    let comVal = comment.value.value
    let params = JSON.stringify({
        comment: comVal
    })
    fetch(http + '/tec/guide/student/log/' + lid, {
        method: 'put',
        headers: {
            "Token": userData.Token,
            'Content-Type': 'application/json; charset=utf-8'
        },
        body:params,
        responseType: 'json',
    }).then((response) => {
        return response.json()
    }).then((data) => {
        if(data.code == 200){
            showHint('success')
        }else{
            showHint('err')
        }
    }).catch((err) => {
        console.log(err);
    })
}

</script>
<style scoped>
.container {
    width: 100%;
    height: 100%;
    background-color: white;
    padding: 4.3%;
    padding-top: 2.9%;
    display: flex;
    flex-direction: column;
    box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.16);
}

.title {
    width: 100%;
    display: flex;
    align-items: flex-end;
    margin-bottom: 18px;
}

.title p:nth-child(1) {
    font-family: Source Han Sans CN;
    font-size: 1.8em;
    font-weight: bolder;
    font-stretch: normal;
    letter-spacing: 0px;
    color: #707070;
}

.title p:nth-child(2) {
    font-family: Source Han Sans CN;
    font-size: 1em;
    font-weight: normal;
    font-stretch: normal;
    line-height: 27px;
    letter-spacing: 0px;
    color: #6f6f6f;
    margin-left: 30px;
}

.content {
    width: 100%;
    font-family: Source Han Sans CN;
    font-size: 18px;
    font-weight: normal;
    font-stretch: normal;
    line-height: 31px;
    letter-spacing: 0px;
    color: #6f6f6f;
    background-color: #ffffff;
    border: solid 1px #8a8a8a;
    padding: 2.5%;
    padding-top: 2%;
    overflow-y: scroll;
    white-space: pre-wrap;
    word-break: break-all;
    height: 312px;
}

/*滚动条整体样式*/
.demo-1::-webkit-scrollbar {
    margin-top: 3%;
    width: 6px;
    /*高宽分别对应横竖滚动条的尺寸*/
    height: 0px;
}

/*滚动条里面小方块*/
.demo-1::-webkit-scrollbar-thumb {
    border-radius: 10px;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    background-color: #d4d4d4;
    opacity: 0.3;
}

/*滚动条里面轨道*/
.demo-1::-webkit-scrollbar-track {
    /* box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2); */
    border-radius: 10px;
}

.subTitle {
    font-family: Source Han Sans CN;
    font-size: 1.5em;
    font-weight: bolder;
    font-stretch: normal;
    line-height: 41px;
    letter-spacing: 0px;
    color: #707070;
    margin-bottom: 1%;
}

.fileBox {
    width: 100%;
    background-color: #ffffff;
    border: solid 1px #8a8a8a;
    padding-left: 3%;
    overflow-y: scroll;
    height: 80px;
}

.fileBox li {
    list-style: none;
    float: left;
    width: 9%;
    margin-right: 1%;
    height: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    justify-content: center;
    flex-direction: column;
    cursor: pointer;
    transition: 0.4;
}
.fileBox li:hover{
    opacity: 0.7;
}

.fileName {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    opacity: 0.8;
    font-size: 12px;
    text-align: center;
}

.fileBox li img {
    opacity: 0.8;
    height: 33px;
}

.but {
    width: 100%;
    height: 30px;
    display: flex;
    justify-content: flex-end;
    margin-top: 27px;
}

.but button {
    width: 160px;
    cursor: pointer;
    height: 30px;
    background-color: #8493a6;
    box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.16);
    border-radius: 6px;
    font-family: Source Han Sans CN;
    font-size: 16px;
    font-weight: normal;
    font-stretch: normal;
    line-height: 27px;
    letter-spacing: 0px;
    color: #fafafa;
    border: 0px;
    transition: 0.2s;
}

.but button:hover {
    background-color: #6f6f6f;
    transition: 0.2s;
}

.commend {
    width: 100%;
    background-color: #ffffff;

}

.commend textarea {
    width: 100%;
    padding: 26px;
    padding-top: 11px;
    font-family: Source Han Sans CN;
    font-size: 18px;
    font-weight: normal;
    font-stretch: normal;
    line-height: 31px;
    letter-spacing: 0px;
    color: #707070;
    outline: none;
    background-color: #ffffff;
    border: solid 1px #8a8a8a;
    resize: none;
    opacity: 0.8;
}

.error {
    top: 85px;
    width: 300px;
    position: fixed;
    z-index: 100;
    left: 50%;
    transform: translate(-50%);
}

</style>
